package com.codewsq.factory;

/**
 * 设计模式：工厂方法模式,保证在不修改原有代码的基础上增强可扩展性
 */
public class FactoryMethodTest {
    public static void main(String[] args) {
        Cars cars = new BenChiFactory().getCars();
        cars.run();

        Cars cars2 = new BMWFactory().getCars();
        cars2.run();
    }
}


interface Cars {
    void run();
}

class BenChi implements Cars {

    @Override
    public void run() {
        System.out.println("奔驰在跑！");
    }
}

class BMW implements Cars {
    @Override
    public void run() {
        System.out.println("宝马在跑！");
    }
}

interface Factory {
    Cars getCars();
}

class BenChiFactory implements Factory {

    @Override
    public Cars getCars() {
        return new BenChi();
    }
}

class BMWFactory implements Factory {

    @Override
    public Cars getCars() {
        return new BMW();
    }
}